﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Components.Data.Fields;
using Components.Data.UI;
using Components.Data.UI.Controls;
using Components.Data.UI.Controls.ControlBuilders;
using Components.Data.UI.MasterPages;

namespace Components.Data.Elements
{
  internal class ElementRepository
  {
    #region Instance implementation

    /// <summary>
    /// Returns a ConfigurationManager instance
    /// </summary>
    public static ElementRepository Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
      // Explicit static constructor to tell C# compiler
      // not to mark type as beforefieldinit
      static Nested()
      {
      }

      public static readonly ElementRepository instance = new ElementRepository();
    }
    #endregion

    public Element Get(String path)
    {
      if (!(path.ToLowerInvariant().Contains("default.aspx") || path.ToLowerInvariant().Contains("contact.aspx")))
        return null;

      Element element = new Element();

      element.Fields = _fieldRepository.GetFields(element.ID);

      element.Presentation.PageBinding = "~/Pages/ContentPage.aspx";

      element.Presentation.MasterPageBindings.Add(new MasterPageBinding("~/Pages/ContentPage.aspx", "~/MasterPages/ContentTwoColumns.master"));
      element.Presentation.MasterPageBindings.Add(new MasterPageBinding("~/MasterPages/ContentTwoColumns.master", "~/MasterPages/Base.Master"));
      element.Presentation.MasterPageBindings.Add(new MasterPageBinding("~/MasterPages/Base.master", ""));

      ControlBindingArgs args;
      ControlBindingFactory controlBindingFactory = new ControlBindingFactory();

      args = new ControlBindingArgs();
      args.Add("VirtualPath", "~/Controls/Header.ascx");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("HeaderControl", "HeaderPlaceHolder", "UserControlBuilder", args));

      args = new ControlBindingArgs();
      args.Add("VirtualPath", "~/Controls/Footer.ascx");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("FooterControl", "FooterPlaceHolder", "UserControlBuilder", args));

      args = new ControlBindingArgs();
      args.Add("AssemblyName", "Components.Website");
      args.Add("TypeName", "Components.Website.Controls.SubNavigation");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("SubNavigationControl", "ColumnTwoPlaceHolder", "ServerControlBuilder", args));

      args = new ControlBindingArgs();
      args.Add("Text", "<div class=\"Title\">This is the title using a label control</div>");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("TitleLabelControl", "ColumnOnePlaceHolder", "LabelControlBuilder", args));

      args = new ControlBindingArgs();
      args.Add("AssemblyName", "System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
      args.Add("TypeName", "System.Web.UI.WebControls.Label");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("IntroductionLabelServerControl", "ColumnOnePlaceHolder", "ServerControlBuilder", args));

      args = new ControlBindingArgs();
      args.Add("DocumentContent", "<?xml version=\"1.0\" encoding=\"utf-8\" ?><content>This is the document XSLT control</content>");
      args.Add("TransformSource", "~/Controls/Document.xslt");
      element.Presentation.ControlBindings.Add(controlBindingFactory.Create("DocumentXmlControl", "ColumnTwoPlaceHolder", "XmlControlBuilder", args));

      return element;
    }

    private FieldRepository _fieldRepository = new FieldRepository();
  }
}
